package com.example.twitterlistener_adr;

import java.io.IOException;

import twitter4j.DirectMessage;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.User;
import twitter4j.UserList;
import twitter4j.UserStreamListener;
import ac.aiit.tw.twFactory;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private boolean _started = false;
	private Button _btn = null;
	private TwitterStream _stream = null;
	private Handler _handler = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this._handler = new Handler();
		setContentView(R.layout.activity_main);
		this._btn = (Button) this.findViewById(R.id.button1);
		this._btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				toggleListener();
				
			}
			
		});
		this.startUser();
	}
	private void logMessage(String msg){
		TextView tv = (TextView) this.findViewById(R.id.textView1);
		tv.append(String.format("%s\n",msg));
	}
	private static class logPoster implements Runnable{
		private String _msg = null;
		private MainActivity _activity = null;
		public logPoster(MainActivity activity, String msg){
			super();
			this._activity = activity;
			this._msg = msg;
		}
		@Override
		public void run() {
			this._activity.logMessage(this._msg);
		}
	}
	private void postLogMessage(String msg){
		this._handler.post(new logPoster(this, msg));
	}
	private void sendMessage(String msg){
		if(this._started){
			this.postLogMessage(msg);
		}
	}
	private void startUser(){
		if(null == this._stream){
			UserStreamListener listener = new UserStreamListener(){

				@Override
				public void onDeletionNotice(StatusDeletionNotice arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onScrubGeo(long arg0, long arg1) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onStallWarning(StallWarning arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onStatus(Status status) {
		        	String msg = status.getText();
		        	sendMessage(msg);
		        }

				@Override
				public void onTrackLimitationNotice(int arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onException(Exception arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onBlock(User arg0, User arg1) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onDeletionNotice(long arg0, long arg1) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onDirectMessage(DirectMessage arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onFavorite(User arg0, User arg1, Status arg2) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onFollow(User arg0, User arg1) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onFriendList(long[] arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onUnblock(User arg0, User arg1) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onUnfavorite(User arg0, User arg1, Status arg2) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onUserListCreation(User arg0, UserList arg1) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onUserListDeletion(User arg0, UserList arg1) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onUserListMemberAddition(User arg0, User arg1,
						UserList arg2) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onUserListMemberDeletion(User arg0, User arg1,
						UserList arg2) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onUserListSubscription(User arg0, User arg1,
						UserList arg2) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onUserListUnsubscription(User arg0, User arg1,
						UserList arg2) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onUserListUpdate(User arg0, UserList arg1) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onUserProfileUpdate(User arg0) {
					// TODO Auto-generated method stub
					
				}
				
			};
		    this._stream = twFactory.getStreamInstance();
		    this._stream.addListener(listener);

		    // sample() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously.
		    this._stream.user();
		    this.logMessage("initialization completed.\n");
		}

	}
	
	private void toggleListener(){
		if(this._started){
			this._btn.setText("start");
			this.logMessage("stopped.");
		}else{
			this._btn.setText("stop");
			this.logMessage("started.");
		}
		this._started = this._started ? false: true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
